home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / net / netamsrc.arc / mac_pt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-12  |  2.9 KB  |  148 lines

  1. #include "global.h"
  2.  
  3.  
  4. /* Given a working directory and an arbitrary pathname, resolve them into
  5.  * an absolute pathname. Memory is allocated for the result, which
  6.  * the caller must free
  7.  */
  8. char *
  9. pathname(cd,path)
  10. char *cd;    /* Current working directory */
  11. char *path;    /* Pathname argument */
  12. {
  13.     register char *buf,*cp;
  14.     char *cdtmp,*pathtmp;
  15.     char BaseVolume[255];
  16.     int iovRef, e;
  17.     char *PtoCstr(), *ptr;
  18.  
  19.     /*
  20.      * Need to determine if the volume name is prepended 
  21.      * and if so remove it.  It will be put back later.
  22.      */
  23.      
  24.     if ( ( ptr = index(cd, ':')) != NULLCHAR)
  25.     {
  26.         sprintf(cd, "%s", ++ptr);
  27.     }
  28.  
  29.     if(cd == NULLCHAR || path == NULLCHAR)
  30.         return NULLCHAR;
  31.         
  32.     /* Strip any leading white space on args */
  33.     while(*cd == ' ' || *cd == '\t')
  34.         cd++;
  35.     while(*path == ' ' || *path == '\t')
  36.         path++;
  37.  
  38.     /* Allocate and initialize output buffer; user must free */
  39.     
  40.     buf = malloc((unsigned)strlen(cd) + strlen(path) + 10);    /* fudge factor */
  41.     
  42.     /* Interpret path relative to cd only if it doesn't begin with ":" */
  43.     
  44.     if(path[0] != ':')
  45.         crunch(buf, cd);
  46.  
  47.     crunch(buf,path);
  48.  
  49.     /* Special case: null final path means the root directory */
  50.     if(buf[0] == '\0'){
  51.         buf[0] = ':';
  52.         buf[1] = '\0';
  53.     }
  54.     /*
  55.      * Now find the Volume name and put it back.
  56.      */
  57.     setmem(BaseVolume,0, sizeof(BaseVolume));
  58.     
  59.     if ( ( e = GetVol(BaseVolume, &iovRef) ) != 0)
  60.     {
  61.         printf("pathname: could not get the volume name.\n");
  62.         path[0] = '\0';
  63.         return(path);
  64.     }
  65.     else
  66.     {
  67.         (void) PtoCstr(BaseVolume);
  68.     }
  69.     
  70.     if ( ( ptr = malloc(strlen(BaseVolume)+strlen(buf))) == NULLCHAR)
  71.     {
  72.         printf("pathname: could not allocate memory.\n");
  73.         return(NULLCHAR);
  74.     }
  75.     else
  76.     {
  77.         sprintf(ptr, "%s%s", BaseVolume, buf);
  78.         free(buf);
  79.     }
  80.     return ptr;
  81. }
  82.  
  83. /* Process a path name string, starting with and adding to
  84.  * the existing buffer
  85.  */
  86. static
  87. crunch(buf,path)
  88. char *buf;
  89. register char *path;
  90. {
  91.     register char *cp;
  92.     
  93.  
  94.     cp = buf + strlen(buf);    /* Start write at end of current buffer */
  95.     
  96.     /* Now start crunching the pathname argument */
  97.     
  98.     while ( *path == ':' )
  99.     {
  100.         /* Hop up a level */
  101.         if((cp = rindex(buf,':')) == NULLCHAR)
  102.         {
  103.             cp = buf;    /* Don't back up beyond root */
  104.             *cp++ = ':';
  105.         }
  106.         *cp = '\0';        /* In case there's another / */
  107.         path++;        /* Skip ":" */
  108.     }
  109.     
  110.     if(*path == '\0')
  111.     {
  112.         *cp++ = '\0';        /* no more, all done */
  113.         return;
  114.     }
  115.     /* Look for parent directory references, either at the end
  116.      * of the path or imbedded in it
  117.      */
  118.     for(;;)
  119.     {
  120.         if(*path == '\0')
  121.         {
  122.             break;        /* no more, all done */
  123.         }
  124.         if(strcmp(path,"::") == 0 )
  125.         {
  126.             /* Hop up a level */
  127.             if((cp = rindex(buf,':')) == NULLCHAR)
  128.                 cp = buf;    /* Don't back up beyond root */
  129.             *cp = '\0';        /* In case there's another :: */
  130.             path += 2;        /* Skip "::" */
  131.             while(*path == ':')    /* Skip one or more slashes */
  132.             {
  133.                 path++;
  134.             }
  135.         }
  136.         else 
  137.         {
  138.             if ( *path == ':' )
  139.                 path++;
  140.             /* Ordinary name, copy up to next '/' or end of path */
  141.             *cp++ = ':';
  142.             while(*path != ':' && *path != '\0')
  143.                 *cp++ = *path++;
  144.         }
  145.     }
  146.     *cp++ = '\0';
  147. }
  148.